# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>

pkgname=sshguard
pkgver=2.4.3
pkgrel=1
pkgdesc="Brute force detector for SSH, Exim, VSFTPD and more. Blocks by ip with iptables"
arch=('loong64' 'x86_64')
url="https://www.sshguard.net/"
license=('GPL')
optdepends=('iptables' 'nftables' 'firewalld' 'ipset')
backup=('etc/sshguard.conf')
source=("https://downloads.sourceforge.net/sourceforge/sshguard/sshguard-$pkgver.tar.gz"
        "tmpfile.conf"
        "sshguard.service")
sha256sums=('64029deff6de90fdeefb1f497d414f0e4045076693a91da1a70eb7595e97efeb'
            'cd2ba683f25a650a29dd465ec7eb507826af46822d1a1ea6116a052c77c4148f'
            '18a5674d8acb9fe2a7bb3ef760b2d7d8e8ad9dbe244b9c762b754c8a482e912a')

prepare() {
  cd "$srcdir/$pkgname-$pkgver"
  sed -i 's|tables.target|tables.service|g' examples/sshguard.service
}

build() {
  cd "$srcdir/$pkgname-$pkgver"
  ./configure \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --sysconfdir=/etc \
    --libexecdir=/usr/lib/sshguard \
    --with-firewall=iptables
  make
}

package() {
  cd "$srcdir/$pkgname-$pkgver"
  make DESTDIR="$pkgdir" install
  install -Dm0644 "$srcdir/tmpfile.conf" "$pkgdir/usr/lib/tmpfiles.d/sshguard.conf"
  install -Dm0644 "$srcdir/sshguard.service" "$pkgdir/usr/lib/systemd/system/sshguard.service"
  install -dm0755 "$pkgdir/usr/share/doc/" "$pkgdir/etc"
  cat <<EOF >"$pkgdir"/etc/sshguard.conf
LOGREADER="LANG=C /usr/bin/journalctl -afb -p info -n1 -t sshd -o cat"
BLACKLIST_FILE=120:/var/db/sshguard/blacklist.db
BACKEND="/usr/lib/sshguard/sshg-fw-iptables"
EOF
  cp -a examples "$pkgdir/usr/share/doc/sshguard"
}
